package com.thirteen.app.controller;


import com.thirteen.app.entity.User;
import com.thirteen.app.service.UserService;
import com.thirteen.app.utils.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户(User)表控制层
 *
 * @author makejava
 * @since 2021-09-03 09:46:42
 */

@CrossOrigin
@Api(tags = "用户管理模块")
@EnableOpenApi
@RestController
@RequestMapping("/api/admin/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;


    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */

    @ApiOperation("根据用户id查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id",
                    required = true, defaultValue = "1"),
    })
    @GetMapping("/select")
    public CommonResult selectOne(User user) {
        return CommonResult.success(userService.queryById(user.getUserId()));
    }

    @ApiOperation("分页查询所有用户的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "分页起始值",
                    required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示多少条记录",
                    required = true, defaultValue = "5", dataType = "integer"),
            @ApiImplicitParam(name = "role", value = "根据角色id查询",
                    dataType = "integer"),
            @ApiImplicitParam(name = "userName", value = "根据用户名称查询",
                    dataType = "string")
    })
    @GetMapping("/page/list")
    public CommonResult getuserPageList(Integer currentPage,
                                        Integer pageSize,
                                        Integer role,
                                        String userName) {
        System.out.println(role);
        Map<String, Object> map = new HashMap<>();
        map.put("total", userService.userCount(role, userName));
        map.put("currentPage", currentPage);
        map.put("data", userService.getuserPageList(currentPage, pageSize, role, userName));
        return CommonResult.success(map);
    }



  /*  @ApiOperation("根据用户id查询对应的日志信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "分页起始值",
                    required = true,defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示多少条记录",
                    required = true,defaultValue = "5", dataType="integer"),
            @ApiImplicitParam(name = "userid",value = "根据用户id查询",
                    dataType="integer"),

    })
    @GetMapping("/event/page/list")
    public ApiRest<Map<String,Object>> getUserEventPageList( Integer currentPage, Integer pageSize,Integer userid){
        Map<String,Object> map=new HashMap<>();
        map.put("currentPage",currentPage);
        map.put("data",userService.getUserEventPageList(currentPage,pageSize,userid));
        return super.success(map);
    }*/

    @ApiOperation("更改用户信息")
    @PostMapping("/updateUser")
    public CommonResult UpdateUser(@RequestBody User user) {
        return CommonResult.success(userService.UpdateUser(user));
    }

    //    @ApiOperation("添加用户")
//    @PostMapping("/edit")
//    public ApiRest<User>  createUser(/*String userName,
//                                     String password,
//                                     String realName,
//                                      Integer collegeId*/
//            @RequestBody User query){
//        User user=new User();
//        user.setUserName(query.getUserName());
//        user.setPassWord(query.getPassWord());
//        user.setRealName(query.getRealName());
//        user.setCollegeId(query.getCollegeId());
//        if(query.getCollegeId()!=null){
//            user.setCollegeId(query.getCollegeId());
//        }
//        user.setRoleId(query.getRoleId());
//
//
//        User newUser=userService.insert(user);
//        Userrole userrole=new Userrole();
//        userrole.setUserId(newUser.getUserId());
//        userrole.setRoleId(Integer.valueOf(query.getRoleId()));
//        userroleService.insert(userrole);
//        return super.success(newUser);
//    }
//
    @ApiOperation("删除用户")
    @PostMapping("/deleteUser")
    public CommonResult deleteUser(@RequestBody User user) {
        return CommonResult.success(userService.deleteById(user.getUserId()));
    }
//
/*   @ApiOperation("改变用户状态")
   @PostMapping("/changeStatus")
   public CommonResult  changeStatus(@RequestBody User user){
       User changeuser=userService.queryById(user.getUserId());
       if(changeuser.getState()==0){
           changeuser.setState(1);
       }else {
           changeuser.setState(0);
       }
       return CommonResult.success(userService.UpdateUser(changeuser));
   }*/


}
